--[[
 Displays range to the target, and displays a target's target window

 Type '/trange' for options, and '/trange o' for additional options

 by Balyn (boggrak@hotmail.com)

 Last update: 11/16/2008

 Release of Target Range

 Version 1.43
]]

--Begin added to play sound file when moving in/out of range
local TargetRangeInRange = nil;
--End added to play sound file when moving in/out of range

local vardb = {};
local varDebug = false;

local TargetTarget_LastTT = nil;
local targetsTargetName = nil;
local varDistance = nil;
local MaxBarValue = 0;

local rangedb;
rangedb = {};
for i= 1,20 do
  rangedb[i] = {};
end


function TargetRange_OnLoad()

  TargetRangeUsers = {};

  SlashCmdList["TARGETRANGE"] = TargetRange_ChatCommand;
  SLASH_TARGETRANGE1 = "/targetrange";
  SLASH_TARGETRANGE2 = "/trange";

  this:RegisterEvent("PLAYER_TARGET_CHANGED");
  this:RegisterEvent("UNIT_TARGET");
  this:RegisterEvent("VARIABLES_LOADED");
  this:RegisterEvent("PLAYER_ALIVE");
  this:RegisterEvent("UNIT_INVENTORY_CHANGED");
  this:RegisterEvent("ACTIONBAR_UPDATE_USABLE");

  TargetRangeTip:SetOwner(WorldFrame, "ANCHOR_NONE");

  if(DEFAULT_CHAT_FRAME) then
    DEFAULT_CHAT_FRAME:AddMessage(TRLocale_Global_Load);
  end

end


function TargetRange_ChatCommand(msg)
  local cmd = msg;
  local pos, _, param = string.find(msg, " (.+)");
  if (param) then
    cmd = string.sub(msg, 1, pos - 1);
  else
    param = "nil";
  end

  if cmd == "help" or cmd == "o" or cmd == "?" then
    for i = 1, table.getn(TARGETRANGE_HELP) do
      TargetRange_ChatMessage(TARGETRANGE_HELP[i]);
    end
  elseif msg == "" then
     TargetRange_OptionsLoad();
     TargetRangeOptions:Show();
  elseif cmd == "status" then
    TargetRange_PrintStatus();
  elseif cmd == "update" then
    TargetRange_SetUpdateInterval(param);
  elseif cmd == "center" then
    TargetRange_Center();
  else
    TargetRange_ChatMessage(TARGETRANGE_UNKNOWN_COMMAND);
  end

end
    

function TargetRange_OnEvent(event)

  HRDebug(event);

  --Begin added to play sound file when moving in/out of range
  if (event == "PLAYER_TARGET_CHANGED")  then
    TargetRangeInRange = nil;
  end
  --End added to play sound file when moving in/out of range

  if (event == "PLAYER_TARGET_CHANGED") or (event == "UNIT_TARGET") then	
    if vardb.targetShow then
      TargetTarget_Update();
    end
    TargetRangeFade();

    if vardb.rangeShow then
      TargetRange_SetRangeVars();
      TargetRange_GetRange();
    end
  end

  if event == "VARIABLES_LOADED" then
    TargetRange_SetupUser();
  end
  if (event == "PLAYER_ALIVE") then
    TargetRange_SetRangeVars();
    TargetRangeFade() ;
  end
  if (event == "UNIT_INVENTORY_CHANGED") then
    TargetRange_SetRangeVars();
  end
  if (event == "ACTIONBAR_UPDATE_USABLE") then
    TargetRange_SetRangeVars();
  end


end
    
local TargetRangeTimeSinceLastUpdate = 0;
vardb.updateInterval = 0.05;

function TargetRange_OnUpdate(self, elapsed)
  TargetRangeTimeSinceLastUpdate = TargetRangeTimeSinceLastUpdate + elapsed;
  if (TargetRangeTimeSinceLastUpdate > vardb.updateInterval) then
    
    if vardb.rangeShow or vardb.targetShow then
      TargetRange_GetRange();
    end

    TargetRangeTimeSinceLastUpdate = 0;
  end

end

function TargetTarget_Update()
    
  if(UnitIsUnit("player", "playertargettarget")) then --targets target is the player        
    TargetRangeSetTargetIsTarget()
  elseif(UnitExists("playertargettarget")) then --targets target is not the player
    TargetRangeSetTargetIsNotTarget();		
  elseif((not UnitExists("playertargettarget"))) then --there is no targets target
    TargetRangeSetNoTarget();		
  end

end

function TargetRangeSetTargetIsTarget() -- set target frame to *** YOU ***
    if(TargetTarget_LastTT ~= "player") then
      targetsTargetName = TRLocale.OTHER.You;
      TargetTarget_LastTT = "player";
      TargetRange_TargetSize(vardb.targetlength);
      if (vardb.showClassColor == false) then
        TargetRangeWatchFrameStatusBar2String2:SetTextColor(1.0, 0.0, 0.0); --Red
--        TargetRangeWatchFrameStatusBar2String2:SetTextColor(1.0, 1.0, 1.0); --White Text
        TargetRangeWatchFrameStatusBar2:SetStatusBarColor(1.0, 0.0, 0.0, vardb.alpha); --Red Bar
      else
        TargetRangeWatchFrameStatusBar2String2:SetTextColor(1.0, 1.0, 1.0); --White
      end
      TargetRangeWatchFrameStatusBar2String2:SetText(targetsTargetName);
    end
end

function TargetRangeSetTargetIsNotTarget() -- set target frame to target's target

    if(TargetTarget_LastTT ~= UnitName("playertargettarget")) then        
      targetsTargetName = UnitName("playertargettarget");
      TargetTarget_LastTT = UnitName("playertargettarget");
      TargetRange_TargetSize(vardb.targetlength);
      TargetRangeWatchFrameStatusBar2String2:SetTextColor(0.0, 1.0, 0.0); --Green Text
      if (vardb.showClassColor == false) then  
        TargetRangeWatchFrameStatusBar2:SetStatusBarColor(0.0, 0.0, 0.0, vardb.alpha); --Black Bar
      end
      TargetRangeWatchFrameStatusBar2String2:SetText(targetsTargetName);
    end

end

function TargetRangeSetNoTarget() -- clear Target frame

    if(TargetTarget_LastTT ~= nil) then
      targetsTargetName = "";
      TargetTarget_LastTT = nil;
      TargetRange_TargetSize(vardb.targetlength);
      TargetRangeWatchFrameStatusBar2String2:SetText(targetsTargetName);
    end

end


function TargetRangeFade() -- Fade frame when no target option

  if (vardb.targetShow) or (vardb.rangeShow) then

    --Window fade when no target option
    if (vardb.windowFade) and (not vardb.windowFriendlyFade) and (vardb.locked) then
      if (UnitExists("playertarget") ) and (UnitIsDeadOrGhost("playertarget") ~= 1) then
        TargetRangeFrame:Show();
        TargetRangeWatchFrame:Show();
      else
        TargetRangeFrame:Hide();
        TargetRangeWatchFrame:Hide();
      end
    end

   --Window fade when friendly target option (also no target)
    if (vardb.windowFriendlyFade) and (vardb.locked) then
      if (UnitExists("playertarget")) and (UnitCanAttack("player", "playertarget")) and (UnitIsDeadOrGhost("playertarget") ~= 1) then
        TargetRangeFrame:Show();
        TargetRangeWatchFrame:Show();
      else
        TargetRangeFrame:Hide();
        TargetRangeWatchFrame:Hide();
      end
    end

    local _, playerClass = UnitClass("playertargettarget");
    local c = RAID_CLASS_COLORS[playerClass];
    if (targetsTargetName == TRLocale.OTHER.You) then c = { r = 1.0, g = 0.0, b = 0.0 } end;
    if (c ~= nil) then
      if (vardb.showClassColor == true) or ( (vardb.showClassColor == false) and (targetsTargetName == TRLocale.OTHER.You) ) then
        if (UnitIsPlayer("playertargettarget")) then
          TargetRangeWatchFrameStatusBar2:SetStatusBarColor(c.r, c.g, c.b, vardb.alpha);
        else
          TargetRangeWatchFrameStatusBar2:SetStatusBarColor(0.5, 0.5, 0.5, vardb.alpha);
        end
      end
    else
      TargetRangeWatchFrameStatusBar2:SetStatusBarColor(0.0, 0.0, 0.0, vardb.alpha);
    end

    _, playerClass = UnitClass("target");
    c = RAID_CLASS_COLORS[playerClass];
    if (c ~= nil)  and (vardb.showClassColor == true) then
      if (UnitIsPlayer("target")) then
        TargetRangeWatchFrameStatusBar1:SetStatusBarColor(c.r, c.g, c.b, vardb.alpha);
      else
        TargetRangeWatchFrameStatusBar1:SetStatusBarColor(0.5, 0.5, 0.5, vardb.alpha);
      end
    else
      TargetRangeWatchFrameStatusBar1:SetStatusBarColor(0.0, 0.0, 0.0, vardb.alpha);
    end

  end

end


function TargetRange_SetupUser()

  HRDebug("func: TargetRange_SetupUser"); 
  local vardbName = UnitName("player") .. "|" .. GetCVar("realmName"); 
  vardb = {};

  if TargetRangeUsers[vardbName] == nil then
    HRDebug("UserDatabse is Nil");
    TargetRangeUsers[vardbName] = { show = true,
       				    alpha = 1.00,
                                  locked = false,
                                  textHeight = 12,
                                  windowFade = false,
                                  windowFriendlyFade = false,
                                  rangeShow = true,
                                  targetShow = true,
                                  targetLength = 9,
                                  rangeLength = 9,
                                  rangeText = true,
                                  showClassColor = true,
                                  updateInterval = 0.15 };
                                     
  end

  vardb = TargetRangeUsers[vardbName];

  if vardb.updateInterval == nil then 
    vardb.updateInterval = 0.15;
  end
  if vardb.targetShow == nil then
    vardb.targetShow = true;
    vardb.alpha = 1.00;
    TargetRange_Center();    
  end
  if vardb.targetLength == nil then
    vardb.targetLength = 9;
  end
  if vardb.rangeShow == nil then
    vardb.rangeShow = true;
    vardb.show = nil;
  end
  if vardb.windowFade == nil then
    vardb.windowFade = false;
  end
  if vardb.windowFriendlyFade == nil then
    vardb.windowFriendlyFade = false;
  end
  if vardb.rangeText == nil then
    vardb.rangeText = true;
  end
  if vardb.rangeLength == nil then vardb.rangeLength = 9 end;
  if vardb.showClassColor == nil then vardb.showClassColor = false end;

  TargetRange_SetAlpha(vardb.alpha);
  TargetRange_SetTextHeight(vardb.textHeight); 
  TargetRange_SetUpdateInterval(vardb.updateInterval);
  TargetRange_TargetSize(vardb.targetlength);
  TargetRange_RangeSize();

  if vardb.locked then
    TargetRange_Lock(false);
  else
    TargetRange_Unlock(false);
  end
  
  if vardb.rangeShow or vardb.targetShow then

    TargetRangeFrame:Show();
    TargetRangeWatchFrame:Show();
    TargetRangeWatchFrameStatusBar1:Show();

    if vardb.rangeShow then
      TargetRangeWatchFrameStatusBar1:Show();
    else
      TargetRangeWatchFrameStatusBar1:Hide();
    end

    if vardb.targetShow then
      TargetRangeWatchFrameStatusBar2:Show();
    else
      TargetRangeWatchFrameStatusBar2:Hide();
    end
  else
    TargetRangeFrame:Hide();
    TargetRangeWatchFrame:Hide();
  end
  TargetRange_SetRangeVars();
  TargetRangeFade() ;
end

function TargetRange_SetRangeVars()

  for i= 1,20 do
    rangedb[i].MinDistance = 0;
    rangedb[i].Distance = 0;
    rangedb[i].Action = 0;
    rangedb[i].Color = "Gray";
    rangedb[i].IsAction = true;
    rangedb[i].InRange = false;
    rangedb[i].IsValid = false;
    rangedb[i].SpellName = "N/A";
    rangedb[i].DisplayName = "N/A";
  end

  local _, playerClass = UnitClass("player");

  if (playerClass == "ROGUE") or (playerClass == "HUNTER") or (playerClass == "WARRIOR") 
      or (playerClass == "MAGE") or (playerClass == "WARLOCK") or (playerClass == "DEATHKNIGHT") then
    rangedb[10].MinDistance = 0;
    rangedb[10].Distance = 7;
    rangedb[10].Action = 3;
    rangedb[10].IsAction = false;
    rangedb[10].Color = "Green";
    rangedb[10].SpellName = "Duel";
    rangedb[10].DisplayName = TRLocale.OTHER.Friend;

    rangedb[11].MinDistance = 7;
    rangedb[11].Distance = 28;
    rangedb[11].Action = 4;
    rangedb[11].IsAction = false;
    rangedb[11].Color = "Yellow";
    rangedb[11].SpellName = "Follow";
    rangedb[11].DisplayName = TRLocale.OTHER.Friend;

    rangedb[12].MinDistance = 28;
    rangedb[12].Distance = 100;
    rangedb[12].Color = "Red";
    rangedb[12].SpellName = "Out of Range";
    rangedb[12].DisplayName = TRLocale.OTHER.Friend;

  end


  if (playerClass == "PRIEST") then

    rangedb[10].MinDistance = 0;
    rangedb[10].Distance = 7;
    rangedb[10].Action = 3;
    rangedb[10].IsAction = false;
    rangedb[10].Color = "Green";
    rangedb[10].SpellName = "Duel";
    rangedb[10].DisplayName = TRLocale.PRIEST.Heal;

    rangedb[11].MinDistance = 7;
    rangedb[11].Distance = 28;
    rangedb[11].Action = 4;
    rangedb[11].IsAction = false;
    rangedb[11].Color = "Yellow";
    rangedb[11].SpellName = "Follow";
    rangedb[11].DisplayName = TRLocale.PRIEST.Heal;

    rangedb[12].MinDistance = 28;
    rangedb[12].Distance = 40;
    rangedb[12].Color = "Orange";
    rangedb[12].SpellName = TRLocale.PRIEST.Heal;
    rangedb[12].DisplayName = TRLocale.OTHER.Max_Heal;

    rangedb[13].MinDistance = 40;
    rangedb[13].Distance = 100;
    rangedb[13].Color = "Red";
    rangedb[13].SpellName = "Out of Range";
    rangedb[13].DisplayName = TRLocale.OTHER.Too_Far;

  end


  if (playerClass == "SHAMAN") then

    rangedb[10].MinDistance = 0;
    rangedb[10].Distance = 7;
    rangedb[10].Action = 3;
    rangedb[10].IsAction = false;
    rangedb[10].Color = "Green";
    rangedb[10].SpellName = "Duel";
    rangedb[10].DisplayName = TRLocale.PRIEST.Heal;

    rangedb[11].MinDistance = 7;
    rangedb[11].Distance = 30;
    rangedb[11].Color = "Yellow";
    rangedb[11].SpellName = TRLocale.SHAMAN.Water_Breathing;
    rangedb[11].DisplayName = TRLocale.PRIEST.Heal;

    rangedb[12].MinDistance = 30;
    rangedb[12].Distance = 40;
    rangedb[12].Color = "Orange";
    rangedb[12].SpellName = TRLocale.SHAMAN.Healing_Wave;
    rangedb[12].DisplayName = TRLocale.OTHER.Max_Heal;

    rangedb[13].MinDistance = 40;
    rangedb[13].Distance = 100;
    rangedb[13].Color = "Red";
    rangedb[13].SpellName = "Out of Range";
    rangedb[13].DisplayName = TRLocale.OTHER.Too_Far;

  end

  if (playerClass == "PALADIN") then

    --Check Holy Shock
    local nameTalent, icon, tier, column, currRank, maxRank = GetTalentInfo(1,18);
    local hasHolyShock = currRank;

    rangedb[10].MinDistance = 0;
    rangedb[10].Distance = 7;
    rangedb[10].Action = 3;
    rangedb[10].IsAction = false;
    rangedb[10].Color = "Green";
    rangedb[10].SpellName = "Duel";
    rangedb[10].DisplayName = TRLocale.PRIEST.Heal;

    if (hasHolyShock == 1) then
      rangedb[11].MinDistance = 7;
      rangedb[11].Distance = 20;
      rangedb[11].Color = "Yellow";
      rangedb[11].SpellName = TRLocale.PALADIN.Holy_Shock;
      rangedb[11].DisplayName = TRLocale.PALADIN.Display_Holy_Shock;
    end

    rangedb[12].MinDistance = 7;
    rangedb[12].Distance = 30;
    rangedb[12].Color = "Yellow";
    if (hasHolyShock == 1) then 
      rangedb[12].MinDistance = 20;
    end
    rangedb[12].SpellName = TRLocale.PALADIN.Blessing_of_Might;
    rangedb[12].DisplayName = TRLocale.PALADIN.Blessing_of_Might_Display;

    rangedb[13].MinDistance = 30;
    rangedb[13].Distance = 40;
    rangedb[13].Color = "Orange";
    rangedb[13].SpellName = TRLocale.PALADIN.Holy_Light;
    rangedb[13].DisplayName = TRLocale.OTHER.Max_Heal;

    rangedb[14].MinDistance = 40;
    rangedb[14].Distance = 100;
    rangedb[14].Color = "Red";
    rangedb[14].SpellName = "Out of Range";
    rangedb[14].DisplayName = TRLocale.OTHER.Too_Far;

  end

  if (playerClass == "DRUID") then

    rangedb[10].MinDistance = 0;
    rangedb[10].Distance = 7;
    rangedb[10].Action = 3;
    rangedb[10].IsAction = false;
    rangedb[10].Color = "Green";
    rangedb[10].SpellName = "Duel";
    rangedb[10].DisplayName = TRLocale.PRIEST.Heal;

    rangedb[11].MinDistance = 7;
    rangedb[11].Distance = 30;
    rangedb[11].Color = "Yellow";
    rangedb[11].SpellName = TRLocale.DRUID.Mark_of_the_Wild;
    rangedb[11].DisplayName = TRLocale.PRIEST.Heal;

    rangedb[12].MinDistance = 30;
    rangedb[12].Distance = 40;
    rangedb[12].Color = "Orange";
    rangedb[12].SpellName = TRLocale.DRUID.Healing_Touch;
    rangedb[12].DisplayName = TRLocale.OTHER.Max_Heal;

    rangedb[13].MinDistance = 40;
    rangedb[13].Distance = 100;
    rangedb[13].Color = "Red";
    rangedb[13].SpellName = "Out of Range";
    rangedb[13].DisplayName = TRLocale.OTHER.Too_Far;

  end



  if playerClass == "ROGUE" then

    --Check Dirty Tricks
    local nameTalent, icon, tier, column, currRank, maxRank = GetTalentInfo(3,5);
    local DirtyTricksBonus = 0;
    if (currRank == 1) then
      DirtyTricksBonus = 2;
    elseif (currRank == 2) then
      DirtyTricksBonus = 5;
    end

    rangedb[1].MinDistance = 0;
    rangedb[1].Distance = 5;
    rangedb[1].Color = "Green";
    rangedb[1].SpellName = TRLocale.ROGUE.Sinister_Strike;
    rangedb[1].DisplayName = TRLocale.OTHER.Melee;

    rangedb[2].MinDistance = 5;
    rangedb[2].Distance = 10 + DirtyTricksBonus;
    rangedb[2].Color = "Yellow";
    rangedb[2].SpellName = TRLocale.ROGUE.Blind;
    rangedb[2].DisplayName = TRLocale.ROGUE.Blind;

    rangedb[3].MinDistance = 10 + DirtyTricksBonus;
    rangedb[3].Distance = 30;
    rangedb[3].Color = "Orange";
    rangedb[3].SpellName = TRLocale.ROGUE.Throw;
    rangedb[3].DisplayName = TRLocale.WARRIOR.Shoot;

    rangedb[4].MinDistance = 30;
    rangedb[4].Distance = 100;
    rangedb[4].Color = "Red";
    rangedb[4].SpellName = TRLocale.ROGUE.Slice_and_Dice;
    rangedb[4].DisplayName = TRLocale.OTHER.Too_Far;

    MaxBarValue = rangedb[4].MinDistance + 5;
  end


  if playerClass == "MAGE" then

    MaxBarValue = 45;

    --Check if mage has the 5 yards extended fire blast range from the gladiator gloves
    local HandsSlotLink = GetInventoryItemLink("player",GetInventorySlotInfo("HandsSlot"));
    local ExtendedRangeGloves;

    if (HandsSlotLink == nil) then
      ExtendedRangeGloves = 0;
    else
      local itemName = GetItemInfo(HandsSlotLink);
      if ((itemName == TRLocale.MAGE.Arena2_Gloves) or (itemName == TRLocale.MAGE.Arena1_Gloves)) then
        ExtendedRangeGloves = 5;
      else
        ExtendedRangeGloves = 0;
      end
    end

    --Check Flame Throwing rank and set number to bonus yards
    local nameTalent, icon, tier, column, currRank, maxRank = GetTalentInfo(2,7);
    local FlameThrowBonus = currRank * 3;

    --Check Arctic Reach rank and set to bonus range percent
    local nameTalent, icon, tier, column, currRank, maxRank = GetTalentInfo(3,11);
    local ArcticBonus = 1 + currRank * .1;

    rangedb[1].MinDistance = 0;
    rangedb[1].Distance = 5;
    rangedb[1].Action = 3;
    rangedb[1].IsAction = false;
    rangedb[1].Color = "Red";
    rangedb[1].SpellName = "Duel";
    rangedb[1].DisplayName = TRLocale.OTHER.Melee;

    rangedb[2].MinDistance = 5;    
    rangedb[2].Distance = 20 + FlameThrowBonus + ExtendedRangeGloves;
    rangedb[2].Color = "Green";
    rangedb[2].SpellName = TRLocale.MAGE.Fire_Blast;
    rangedb[2].DisplayName = TRLocale.MAGE.Fire_Blast;

    if (ExtendedRangeGloves == 0) then
      rangedb[3].MinDistance = rangedb[2].Distance;
      rangedb[3].Distance = 30;
      rangedb[3].Color = "Green";
      rangedb[3].SpellName = TRLocale.MAGE.Arcane_Missiles;
      rangedb[3].DisplayName = TRLocale.MAGE.Arcane_Missiles_Display;
    else
      rangedb[3].Distance = rangedb[2].Distance;
    end

    if (FlameThrowBonus > 0) then
      rangedb[4].MinDistance = rangedb[3].Distance;
      rangedb[4].Distance = 30 + FlameThrowBonus;
      rangedb[4].Color = "Yellow";
      rangedb[4].SpellName = TRLocale.MAGE.Scorch;
      rangedb[4].DisplayName = TRLocale.MAGE.Scorch;
    elseif (ArcticBonus > 1) then
      rangedb[4].MinDistance = rangedb[3].Distance;
      rangedb[4].Distance = 30 * ArcticBonus;
      rangedb[4].Color = "Yellow";
      rangedb[4].SpellName = TRLocale.MAGE.Frostbolt;
      rangedb[4].DisplayName = TRLocale.MAGE.Frostbolt;
    end

    rangedb[5].MinDistance = rangedb[3].Distance;
    if (FlameThrowBonus > 0) or (ArcticBonus > 1) then 
      rangedb[5].MinDistance = rangedb[4].Distance;
    end
    rangedb[5].Distance = 35 + FlameThrowBonus;
    rangedb[5].Color = "Orange";
    rangedb[5].SpellName = TRLocale.MAGE.Fireball;
    rangedb[5].DisplayName = TRLocale.MAGE.Fireball;

    if (FlameThrowBonus == 0) and (ArcticBonus == 1) then 
      rangedb[6].MinDistance = 35;
      rangedb[6].Distance = 40;
      rangedb[6].Color = "Blue";
      rangedb[6].SpellName = TRLocale.MAGE.Detect_Magic;
      rangedb[6].DisplayName = TRLocale.MAGE.Detect_Magic;
    end

    rangedb[7].MinDistance = 40;
    if (FlameThrowBonus > 0) or (ArcticBonus > 1) then 
      rangedb[7].MinDistance = rangedb[5].Distance;
    end
    rangedb[7].Distance = 100;
    rangedb[7].Color = "Red";
    rangedb[7].SpellName = "Out of Range";
    rangedb[7].DisplayName = TRLocale.OTHER.Too_Far;

    MaxBarValue = rangedb[7].MinDistance + 5;  

  end


  if playerClass == "HUNTER" then

    --Check Hawkeye rank and set to number of bonus yards
    local nameTalent, icon, tier, column, currRank, maxRank = GetTalentInfo(3,2);
    local HawkeyeBonus = currRank * 2;

    --Check ScatterShot
    local nameTalent, icon, tier, column, currRank, maxRank = GetTalentInfo(3,9);
    local ScatterShot = currRank;

    rangedb[1].MinDistance = 0;
    rangedb[1].Distance = 5;
    rangedb[1].Color = "Orange";
    rangedb[1].SpellName = TRLocale.HUNTER.Wing_Clip;
    rangedb[1].DisplayName = TRLocale.OTHER.Melee;

--    rangedb[2].MinDistance = 5;
--    rangedb[2].Distance = 6;
--    rangedb[2].Action = 3;
--    rangedb[2].Color = "Red";
--    rangedb[2].SpellName = TRLocale.OTHER.Deadzone;
--    rangedb[2].DisplayName = TRLocale.OTHER.Deadzone;

    rangedb[3].MinDistance = 5;
    rangedb[3].Distance = 15 + HawkeyeBonus;
    rangedb[3].Color = "Yellow";

    if (ScatterShot == 0) then 
      rangedb[3].SpellName = "N/A";
      rangedb[4].MinDistance = 5;
    else
      rangedb[3].SpellName = TRLocale.HUNTER.Scatter_Shot;
      rangedb[3].DisplayName = TRLocale.HUNTER.Scatter_Shot;
      rangedb[4].MinDistance = rangedb[3].Distance;
    end

    rangedb[4].Distance = 28;
    rangedb[4].Action = 4;
    rangedb[4].IsAction = false;
    rangedb[4].Color = "Green";
    rangedb[4].SpellName = "Can Follow";
    rangedb[4].DisplayName = TRLocale.WARRIOR.Shoot;

    rangedb[5].MinDistance = 28;
    rangedb[5].Distance = 35 + HawkeyeBonus;
    rangedb[5].Color = "Orange";
    rangedb[5].SpellName = TRLocale.HUNTER.Auto_Shot;
    rangedb[5].DisplayName = TRLocale.WARRIOR.Shoot;

    rangedb[6].MinDistance = rangedb[5].Distance;
    rangedb[6].Distance = 100;
    rangedb[6].Color = "Red";
    rangedb[6].SpellName = TRLocale.HUNTER.Hunters_Mark;
    rangedb[6].DisplayName = TRLocale.HUNTER.Hunters_Mark_Display;

    MaxBarValue = rangedb[6].MinDistance + 5;  

  end


  if playerClass == "WARRIOR" then

    rangedb[1].MinDistance = 0;
    rangedb[1].Distance = 5;
    rangedb[1].Color = "Green";
    rangedb[1].SpellName = TRLocale.WARRIOR.Hamstring;
    rangedb[1].DisplayName = TRLocale.OTHER.Melee;

    rangedb[2].MinDistance = 5;
    rangedb[2].Distance = 10;
    rangedb[2].Color = "Yellow";
    rangedb[2].SpellName = TRLocale.WARRIOR.Intimidating_Shout;
    rangedb[2].DisplayName = TRLocale.WARRIOR.Intimidating_Shout_Display;

    rangedb[3].MinDistance = 10;
    rangedb[3].Distance = 25;
    rangedb[3].Color = "Blue";
    rangedb[3].SpellName = TRLocale.WARRIOR.Charge;
    rangedb[3].DisplayName = TRLocale.WARRIOR.Charge;

    rangedb[4].MinDistance = 25;
    rangedb[4].Distance = 30;
    rangedb[4].Color = "Orange";
    rangedb[4].SpellName = TRLocale.WARRIOR.Shoot;
    rangedb[4].DisplayName = TRLocale.WARRIOR.Shoot;

    rangedb[5].MinDistance = 30;
    rangedb[5].Distance = 100;
    rangedb[5].Color = "Red";
    rangedb[5].SpellName = "Out of Range";
    rangedb[5].DisplayName = TRLocale.OTHER.Too_Far;
  
    MaxBarValue = rangedb[5].MinDistance + 5;

  end

  if playerClass == "DEATHKNIGHT" then

    rangedb[1].MinDistance = 0;
    rangedb[1].Distance = 5;
    rangedb[1].Color = "Green";
    rangedb[1].SpellName = TRLocale.DEATHKNIGHT.Blood_Strike;
    rangedb[1].DisplayName = TRLocale.OTHER.Melee;

    rangedb[2].MinDistance = 5;
    rangedb[2].Distance = 20;
    rangedb[2].Color = "Yellow";
    rangedb[2].SpellName = TRLocale.DEATHKNIGHT.Icy_Touch;
    rangedb[2].DisplayName = TRLocale.DEATHKNIGHT.Icy_Touch;

    rangedb[3].MinDistance = 20;
    rangedb[3].Distance = 30;
    rangedb[3].Color = "Orange";
    rangedb[3].SpellName = TRLocale.DEATHKNIGHT.Death_Coil;
    rangedb[3].DisplayName = TRLocale.DEATHKNIGHT.Death_Coil;

    rangedb[4].MinDistance = 30;
    rangedb[4].Distance = 100;
    rangedb[4].Color = "Red";
    rangedb[4].SpellName = "Out of Range";
    rangedb[4].DisplayName = TRLocale.OTHER.Too_Far;
  
    MaxBarValue = rangedb[4].MinDistance + 5;

  end
  
  if playerClass == "WARLOCK" then

    --Check Grim Reach rank and set to bonus range percent
    local nameTalent, icon, tier, column, currRank, maxRank = GetTalentInfo(1,11);
    local GrimBonus = 1 + currRank * .1;

    --Check Destructive Reach rank and set to bonus range percent
    local nameTalent, icon, tier, column, currRank, maxRank = GetTalentInfo(3,10);
    local DestrBonus = 1 + currRank * .1;

    rangedb[1].MinDistance = 0;
    rangedb[1].Distance = 5;
    rangedb[1].Action = 3;
    rangedb[1].IsAction = false;
    rangedb[1].Color = "Green";
    rangedb[1].SpellName = "Duel";
    rangedb[1].DisplayName = TRLocale.OTHER.Melee;

    rangedb[2].MinDistance = 5;
    rangedb[2].Distance = 20 * GrimBonus;
    rangedb[2].Color = "Blue";
    rangedb[2].SpellName = TRLocale.WARLOCK.Fear;
    rangedb[2].DisplayName = TRLocale.WARLOCK.Fear;

    if (GrimBonus > 1) and (DestrBonus == 1) then 
      rangedb[3].MinDistance = rangedb[2].Distance;
      rangedb[3].Distance = 30;
      rangedb[3].Color = "Yellow";
      rangedb[3].SpellName = TRLocale.WARLOCK.Immolate;
      rangedb[3].DisplayName = TRLocale.WARLOCK.Immolate_Display;

      rangedb[4].MinDistance = 30;
      rangedb[4].Distance = 30 * GrimBonus;
      rangedb[4].Color = "Orange";
      rangedb[4].SpellName = TRLocale.WARLOCK.Corruption;
      rangedb[4].DisplayName = TRLocale.WARLOCK.Corruption_Display;

      rangedb[5].MinDistance = rangedb[4].Distance;
      rangedb[5].Distance = 100;
      rangedb[5].Color = "Red";
      rangedb[5].SpellName = "Out of Range";
      rangedb[5].DisplayName = TRLocale.OTHER.Too_Far;
    elseif (DestrBonus > 1) and (GrimBonus == 1) then
      rangedb[3].MinDistance = rangedb[2].Distance;
      rangedb[3].Distance = 30;
      rangedb[3].Color = "Yellow";
      rangedb[3].SpellName = TRLocale.WARLOCK.Corruption;
      rangedb[3].DisplayName = TRLocale.WARLOCK.Corruption_Display;

      rangedb[4].MinDistance = 30;
      rangedb[4].Distance = 30 * DestrBonus;
      rangedb[4].Color = "Orange";
      rangedb[4].SpellName = TRLocale.WARLOCK.Immolate;
      rangedb[4].DisplayName = TRLocale.WARLOCK.Immolate_Display;

      rangedb[5].MinDistance = rangedb[4].Distance;
      rangedb[5].Distance = 100;
      rangedb[5].Color = "Red";
      rangedb[5].SpellName = "Out of Range";
      rangedb[5].DisplayName = TRLocale.OTHER.Too_Far;
      MaxBarValue = rangedb[5].MinDistance + 5;
    else
      rangedb[3].MinDistance = rangedb[2].Distance;
      rangedb[3].Distance = 30 * DestrBonus;
      rangedb[3].Color = "Yellow";
      rangedb[3].SpellName = TRLocale.WARLOCK.Shadow_Bolt;
      rangedb[3].DisplayName = TRLocale.WARLOCK.Shadow_Bolt;

      rangedb[4].MinDistance = rangedb[3].Distance;
      rangedb[4].Distance = 100;
      rangedb[4].Color = "Red";
      rangedb[4].SpellName = "Out of Range";
      rangedb[4].DisplayName = TRLocale.OTHER.Too_Far;
      MaxBarValue = 45;
    end

  end

  
  if playerClass == "PRIEST" then

    --Check Mind Flay
    local nameTalent, icon, tier, column, currRank, maxRank = GetTalentInfo(3,9);
    local MindFlay = (currRank == 1);

    --Check Shadow Reach rank and set to bonus range percent
    local nameTalent, icon, tier, column, currRank, maxRank = GetTalentInfo(3,11);
    local ShadowReachBonus = 1 + currRank * .1;

    rangedb[1].MinDistance = 0;
    rangedb[1].Distance = 5;
    rangedb[1].Action = 3;
    rangedb[1].IsAction = false;
    rangedb[1].Color = "Green";
    rangedb[1].SpellName = "Duel";
    rangedb[1].DisplayName = TRLocale.OTHER.Melee;

    if (MindFlay) then
      rangedb[2].MinDistance = 5;
      rangedb[2].Distance = 20 * ShadowReachBonus;
      rangedb[2].Color = "Yellow";
      rangedb[2].SpellName = TRLocale.PRIEST.Mind_Flay;
      rangedb[2].DisplayName = TRLocale.PRIEST.Mind_Flay;
    else
      rangedb[2].MinDistance = 5;
      rangedb[2].Distance = 20 * ShadowReachBonus;
      rangedb[2].Color = "Yellow";
      rangedb[2].SpellName = TRLocale.PRIEST.Mind_Control;
      rangedb[2].DisplayName = TRLocale.PRIEST.Mind_Control;
    end
   
    if (ShadowReachBonus > 1) then 
      rangedb[3].MinDistance = rangedb[2].Distance;
      rangedb[3].Distance = 30;
      rangedb[3].Color = "Orange";
      rangedb[3].SpellName = TRLocale.WARRIOR.Shoot;
      rangedb[3].DisplayName = TRLocale.WARRIOR.Shoot;
    end

    if (ShadowReachBonus > 1) then 
      rangedb[4].MinDistance = rangedb[3].Distance;
    else 
      rangedb[4].MinDistance = rangedb[2].Distance;
    end
    rangedb[4].Distance = 30 * ShadowReachBonus;
    rangedb[4].Color = "Orange";
    rangedb[4].SpellName = TRLocale.PRIEST.Mind_Blast;
    rangedb[4].DisplayName = TRLocale.PRIEST.Mind_Blast;

    rangedb[5].MinDistance = rangedb[4].Distance;
    rangedb[5].Distance = 100;
    rangedb[5].Color = "Red";
    rangedb[5].SpellName = TRLocale.PRIEST.Mind_Vision;
    rangedb[5].DisplayName = TRLocale.OTHER.Too_Far;

    MaxBarValue = 45;

  end


  if playerClass == "SHAMAN" then

    --Check Storm Reach rank and set number to bonus yards
    local nameTalent, icon, tier, column, currRank, maxRank = GetTalentInfo(1,11);
    local StormReachBonus = currRank * 3;
    
    --Check if shaman has the 5 yards extended shock range from the gladiator gloves
    local ExtendedRangeGloves;
    local HandsSlotLink = GetInventoryItemLink("player",GetInventorySlotInfo("HandsSlot"));

    if (HandsSlotLink == nil) then
      ExtendedRangeGloves = 0;
    else
      local itemName = GetItemInfo(HandsSlotLink);
      if ( 
           (itemName == TRLocale.SHAMAN.Arena2_Gloves) or (itemName == TRLocale.SHAMAN.Arena1_Gloves) or 
           (itemName == TRLocale.SHAMAN.Arena3Version1_Gloves) or (itemName == TRLocale.SHAMAN.Arena3Version2_Gloves) or
           (itemName == TRLocale.SHAMAN.Arena4Version1_Gloves) or (itemName == TRLocale.SHAMAN.Arena4Version2_Gloves) or
           (itemName == TRLocale.SHAMAN.Arena5Version1_Gloves) or (itemName == TRLocale.SHAMAN.Arena5Version2_Gloves) or
           (itemName == TRLocale.SHAMAN.Arena5Version3_Gloves) or (itemName == TRLocale.SHAMAN.Arena4Version3_Gloves) 
      )  then
        ExtendedRangeGloves = 5;
      else
        ExtendedRangeGloves = 0;
      end
    end
    --DEFAULT_CHAT_FRAME:AddMessage(itemName);

    rangedb[1].MinDistance = 0;
    rangedb[1].Distance = 5;
    rangedb[1].Action = 3;
    rangedb[1].IsAction = false;
    rangedb[1].Color = "Green";
    rangedb[1].SpellName = "Duel";
    rangedb[1].DisplayName = TRLocale.OTHER.Melee;

    rangedb[2].MinDistance = 5;
    rangedb[2].Distance = 20 + ExtendedRangeGloves;
    rangedb[2].Color = "Yellow";
    rangedb[2].SpellName = TRLocale.SHAMAN.Earth_Shock;
    rangedb[2].DisplayName = TRLocale.SHAMAN.Earth_Shock_Display;

    rangedb[3].MinDistance = rangedb[2].Distance;
    rangedb[3].Distance = 30 + StormReachBonus;
    rangedb[3].Color = "Orange";
    rangedb[3].SpellName = TRLocale.SHAMAN.Lightning_Bolt;
    rangedb[3].DisplayName = TRLocale.SHAMAN.Lightning_Bolt_Display;

    rangedb[4].MinDistance = rangedb[3].Distance;
    rangedb[4].Distance = 100;
    rangedb[4].Color = "Red";
    rangedb[4].SpellName = "Out of Range";
    rangedb[4].DisplayName = TRLocale.OTHER.Too_Far;

    MaxBarValue = 45;

  end  

  if playerClass == "PALADIN" then

    --Check Crusader Strike 
    local nameTalent, icon, tier, column, currRank, maxRank = GetTalentInfo(3,24);
    local hasCrusaderStrike = currRank;
    --Check Repentance 
    local nameTalent, icon, tier, column, currRank, maxRank = GetTalentInfo(3,19);
    local hasRepentance = currRank;

    if (hasCrusaderStrike == 1) then
      rangedb[1].MinDistance = 0;
      rangedb[1].Distance = 5;
      rangedb[1].Color = "Green";
      rangedb[1].SpellName = TRLocale.PALADIN.Crusader_Strike;
      rangedb[1].DisplayName = TRLocale.OTHER.Melee;
    else
      rangedb[1].MinDistance = 0;
      rangedb[1].Distance = 5;
      rangedb[1].Action = 3;
      rangedb[1].IsAction = false;
      rangedb[1].Color = "Green";
      rangedb[1].SpellName = "Duel";
      rangedb[1].DisplayName = TRLocale.OTHER.Melee;
    end

    rangedb[2].MinDistance = 5;
    rangedb[2].Distance = 10;
    rangedb[2].Color = "Yellow";
    rangedb[2].SpellName = TRLocale.PALADIN.Hammer_of_Justice;
    rangedb[2].DisplayName = TRLocale.PALADIN.Hammer_of_Justice_Display;

    if (((hasRepentance == 1) and (UnitCreatureType("target") == "Humanoid")) or (hasHolyShock == 1)) then
      rangedb[3].MinDistance = 10;
      rangedb[3].Distance = 20;
      rangedb[3].Color = "Orange";
      if (hasRepentance == 1) then
        rangedb[3].SpellName = TRLocale.PALADIN.Repentance;
        rangedb[3].DisplayName = TRLocale.PALADIN.Repentance;
      end
      if (hasHolyShock == 1) then
        rangedb[3].SpellName = TRLocale.PALADIN.Holy_Shock;
        rangedb[3].DisplayName = TRLocale.PALADIN.Holy_Shock;
      end
    end

    rangedb[4].MinDistance = 10;
    if (((hasRepentance == 1) and (UnitCreatureType("target") == "Humanoid")) or (hasHolyShock == 1)) then
      rangedb[4].MinDistance = 20;
    end
    rangedb[4].Distance = 30;
    rangedb[4].Color = "Orange";
    rangedb[4].SpellName = TRLocale.PALADIN.Hammer_of_Wrath;
    rangedb[4].DisplayName = TRLocale.PALADIN.Hammer_of_Wrath_Display;

    rangedb[5].MinDistance = 30;
    rangedb[5].Distance = 100;
    rangedb[5].Color = "Red";
    rangedb[5].SpellName = "Out of Range";
    rangedb[5].DisplayName = TRLocale.OTHER.Too_Far;
  
    MaxBarValue = 45;

  end

  if playerClass == "DRUID" then

    --Check Feral Charge
    local nameTalent, icon, tier, column, currRank, maxRank = GetTalentInfo(2,14);
    local hasFeralCharge = currRank;
   
    --Check Nature's Reach
    local nameTalent, icon, tier, column, currRank, maxRank = GetTalentInfo(1,9);
    local NaturesReachBonus = 1 + currRank * .1;

    local nStance = GetShapeshiftForm(true);
    -- 1 = Bear/Dire Bear Form
    -- 2 = Aquatic Form
    -- 3 = Cat Form
    -- 4 = Travel Form
    -- 5 = Moonkin/Tree Form (flight form)
    -- 6 = Flight Form (?)

    if (nStance == 1) then
      rangedb[1].MinDistance = 0;
      rangedb[1].Distance = 5;
      rangedb[1].Color = "Green";
      rangedb[1].SpellName = TRLocale.DRUID.Bash ;
      rangedb[1].DisplayName = TRLocale.OTHER.Melee;
    elseif (nStance == 3) then
      rangedb[1].MinDistance = 0;
      rangedb[1].Distance = 5;
      rangedb[1].Color = "Green";
      rangedb[1].SpellName = TRLocale.DRUID.Claw;
      rangedb[1].DisplayName = TRLocale.OTHER.Melee;
    else
      rangedb[1].MinDistance = 0;
      rangedb[1].Distance = 5;
      rangedb[1].Action = 3;
      rangedb[1].IsAction = false;
      rangedb[1].Color = "Green";
      rangedb[1].SpellName = "Duel";
      rangedb[1].DisplayName = TRLocale.OTHER.Melee;
    end

    if (hasFeralCharge == 1) and (nStance == 1) then
      rangedb[2].MinDistance = 5;
      rangedb[2].Distance = 8;
      rangedb[2].Color = "Red";
      rangedb[2].SpellName = TRLocale.OTHER.Deadzone;
      rangedb[2].DisplayName = TRLocale.OTHER.Deadzone;

      rangedb[3].MinDistance = 8;
      rangedb[3].Distance = 25;
      rangedb[3].Color = "Yellow";
      rangedb[3].SpellName = TRLocale.DRUID.Feral_Charge;
      rangedb[3].DisplayName = TRLocale.DRUID.Feral_Charge;

--      if (hasFeralFaerie == 1) then
--        rangedb[4].MinDistance = 25;
--        rangedb[4].Distance = 30 * NaturesReachBonus;
--        rangedb[4].Color = "Orange";
--        rangedb[4].SpellName = TRLocale.DRUID.Faerie_Fire;
--        rangedb[4].DisplayName = TRLocale.DRUID.Faerie_Fire;
--      else
        rangedb[4].MinDistance = 25;
        rangedb[4].Distance = 30 * NaturesReachBonus;
        rangedb[4].Color = "Orange";
        rangedb[4].SpellName = TRLocale.DRUID.Wrath;
        rangedb[4].DisplayName = TRLocale.OTHER.Too_Far;
--      end
    elseif (nStance == 3) then
--      if (hasFeralFaerie == 1) then
--        rangedb[4].MinDistance = 5;
--        rangedb[4].Distance = 30 * NaturesReachBonus;
--        rangedb[4].Color = "Orange";
--        rangedb[4].SpellName = TRLocale.DRUID.Faerie_Fire;
--        rangedb[4].DisplayName = TRLocale.DRUID.Faerie_Fire;
--      else
        rangedb[4].MinDistance = 5;
        rangedb[4].Distance = 30 * NaturesReachBonus;
        rangedb[4].Color = "Red";
        rangedb[4].SpellName = TRLocale.DRUID.Wrath;
        rangedb[4].DisplayName = TRLocale.OTHER.Too_Far;
--      end
    else 
      rangedb[4].MinDistance = 5;
      rangedb[4].Distance = 30 * NaturesReachBonus;
      rangedb[4].Color = "Yellow";
      rangedb[4].SpellName = TRLocale.DRUID.Wrath;
      rangedb[4].DisplayName = TRLocale.DRUID.Wrath;
    end

    rangedb[5].MinDistance = 30 * NaturesReachBonus;
    rangedb[5].Distance = 100;
    rangedb[5].Color = "Red";
    rangedb[5].SpellName = "Out of Range";
    rangedb[5].DisplayName = TRLocale.OTHER.Too_Far;

    MaxBarValue = 45;

  end



end

function TargetRange_GetRange()

-------------------------------
  local varMin = 0;
  local varMax = 0;
  local rangeColor = "";
  local rangeDisplayName = "";
  local startLoop = 1;
  local endLoop = 7;

  if not (UnitCanAttack("player", "playertarget")) then
    startLoop = 10;
    endLoop = 20;
  end

  --Action is in range
  for i= startLoop,endLoop do
    rangedb[i].InRange = false;
    rangedb[i].IsValid = false;

    if rangedb[i].IsAction  then
      local varInRange = IsSpellInRange(rangedb[i].SpellName);
      if (varInRange ~= nil) and (UnitIsDeadOrGhost("playertarget") ~= 1) then
        rangedb[i].IsValid = true;
        rangedb[i].InRange = (varInRange == 1); 
      end
--Hunter deadzone removed
--      if (rangedb[i].SpellName == TRLocale.OTHER.Deadzone) then
--        if (IsSpellInRange(TRLocale.HUNTER.Wing_Clip) ~= 1) 
--          and (IsSpellInRange(TRLocale.HUNTER.Auto_Shot) ~= 1)
--          and (CheckInteractDistance("target", 1) == 1) 
--          and (UnitIsDeadOrGhost("playertarget") ~= 1) then
--            rangedb[i].IsValid = true;
--            rangedb[i].InRange = true;
--        end
--      end
      if (rangedb[i].SpellName == TRLocale.OTHER.Deadzone) then
        if (IsSpellInRange(TRLocale.DRUID.Bash) ~= 1) 
          and (IsSpellInRange(TRLocale.DRUID.Feral_Charge) ~= 1)
          and (CheckInteractDistance("target", 2) == 1) 
          and (IsSpellInRange(TRLocale.DRUID.Wrath) == 1)
          and (UnitIsDeadOrGhost("playertarget") ~= 1) then
            rangedb[i].IsValid = true;
            rangedb[i].InRange = true;
        end
      end
      if (rangedb[i].SpellName == "Out of Range") and (UnitIsDeadOrGhost("playertarget") ~= 1) and (UnitName("playertarget") ~= nil) and (UnitName("playertarget") ~= "Unknown") then
        rangedb[i].IsValid = true;
        rangedb[i].InRange = true;        
--DEFAULT_CHAT_FRAME:AddMessage(UnitName("playertarget") );
      end 
    else
      local varInRange = CheckInteractDistance("target", rangedb[i].Action);
      rangedb[i].IsValid = true;
      if (varInRange ~= nil) and (UnitIsDeadOrGhost("playertarget") ~= 1) then
        rangedb[i].InRange =  (varInRange == 1);
      end
    end    

  end


  local rangeSpellName;

  for i = endLoop,startLoop,-1 do
    if rangedb[i].IsValid then
      if rangedb[i].InRange then
        varMin = rangedb[i].MinDistance;
        varMax = rangedb[i].Distance;
        rangeColor = rangedb[i].Color;
        rangeDisplayName = rangedb[i].DisplayName;
	rangeSpellName = rangedb[i].SpellName;
      end
    end  
  end


  --Begin added to play sound file when moving in/out of range
  if ((rangeSpellName == "Out of Range") or (rangeSpellName == TRLocale.HUNTER.Hunters_Mark)) and (TargetRangeInRange ~= false) then    
    TargetRangeInRange = false;
    --Place call to out of range sound file here
  end
  if (varMax ~= 0) and (rangeSpellName ~= "Out of Range") and (rangeSpellName ~= TRLocale.HUNTER.Hunters_Mark) and (TargetRangeInRange ~= true) then
    TargetRangeInRange = true;
    --Place call to in range sound file here
  end
  --End added to play sound file when moving in/out of range

  local c1, c2, c3;
  varDistance = "";
  if (varMax > 0) then
    if vardb.rangeText then
      varDistance = varMin.."-"..varMax.." ("..rangeDisplayName..")";
    else
      varDistance = varMin.."-"..varMax;   
    end

    if rangeColor == "Green" then
      c1=0.0; c2=1.0; c3=0.0;
    elseif rangeColor == "Yellow" then
      c1=1.0; c2=1.0; c3=0.0;
    elseif rangeColor == "Red" then
      c1=1.0; c2=0.0; c3=0.0;
    elseif rangeColor == "Blue" then
      c1=0.0; c2=0.5; c3=0.5;
    elseif rangeColor == "Orange" then
      c1=1.0; c2=0.5; c3=0.0;
    else
      c1=0.0; c2=0.0; c3=0.0; --Black
    end

  else
      c1=0.0; c2=0.0; c3=0.0; --Black
  end

  TargetRangeWatchFrameStatusBar1:SetMinMaxValues(0, MaxBarValue);
  local rangeBarValue = 0;
  if (varMin > 0) and (varMax > 0) then
    rangeBarValue = (varMin + varMax) / 2;
  else
    rangeBarValue = varMax;
  end;
--  TargetRangeWatchFrameStatusBar1:SetValue(min(rangeBarValue, MaxBarValue));
--  revert this back for new release. I just want full bar personally for now
  TargetRangeWatchFrameStatusBar1:SetValue(100);

  if (vardb.showClassColor == false) then
    TargetRangeWatchFrameStatusBar2String2:SetTextColor(1.0, 1.0, 1.0);
  else
    TargetRangeWatchFrameStatusBar1:SetStatusBarColor(c1, c2, c3, vardb.alpha);
  end
  
  --[[if (vardb.showClassColor == false) then
    TargetRangeWatchFrameStatusBar1:SetStatusBarColor(c1, c2, c3, vardb.alpha);
  else
    TargetRangeWatchFrameStatusBar2String2:SetTextColor(1.0, 1.0, 1.0);
  end]]

  TargetRange_RangeSize();
  TargetRangeWatchFrameStatusBar1String1:SetText(varDistance);

end


function TargetRange_TargetSize(aTargetLength)
  
  local DefaultNameLength = "";
  for i= 1,vardb.targetLength do
    DefaultNameLength = DefaultNameLength.."X";
  end
  local DefaultNameWidth = TargetRange_GetTextWidth(DefaultNameLength);
  local NameTextWidth = TargetRange_GetTextWidth(targetsTargetName);

  if targetsTargetName ~= nil and NameTextWidth > DefaultNameWidth then
    TargetRangeWatchFrameStatusBar2:SetWidth(NameTextWidth + 20);
  else
    TargetRangeWatchFrameStatusBar2:SetWidth(DefaultNameWidth + 20);
  end

end

function TargetRange_RangeSize()
  
  local DefaultRangeLength = "";
  for i= 1,vardb.rangeLength do
    DefaultRangeLength = DefaultRangeLength.."X";
  end
  local DefaultRangeWidth = TargetRange_GetTextWidth(DefaultRangeLength);
  local RangeTextWidth = TargetRange_GetTextWidth(varDistance);

  if (varDistance ~= nil) and RangeTextWidth > DefaultRangeWidth then
    TargetRangeWatchFrameStatusBar1:SetWidth(RangeTextWidth + 20);
  else
    TargetRangeWatchFrameStatusBar1:SetWidth(DefaultRangeWidth + 20);
  end

end

function TargetRange_PrintStatus()
    
  for i= 1,7 do
    if rangedb[i].DisplayName ~= "N/A" then
      TargetRange_ChatMessage("Using: "..rangedb[i].SpellName.." Range: "..rangedb[i].MinDistance.."-"..rangedb[i].Distance);
    end
  end 
      
  TargetRange_ChatMessage("FontSize = "..vardb.textHeight);
  TargetRange_ChatMessage("Frame Alpha = "..vardb.alpha);
  TargetRange_ChatMessage("Update Interval = "..vardb.updateInterval);
  TargetRange_ChatMessage("Target Window Size = "..vardb.targetLength);
  TargetRange_ChatMessage("Range Window Size = "..vardb.rangeLength);
  if vardb.locked then
    TargetRange_ChatMessage("Frame Locked");
  else
    TargetRange_ChatMessage("Frame Not Locked");
  end
  if vardb.windowFade then
    TargetRange_ChatMessage("Window Fade: On");  
  else
    TargetRange_ChatMessage("Window Fade: Off");
  end    
  if vardb.windowFriendlyFade then
    TargetRange_ChatMessage("Friendly Window Fade: On");  
  else
    TargetRange_ChatMessage("Friendly Window Fade: Off");
  end    

  if vardb.targetShow or vardb.rangeShow then
    if vardb.rangeShow then
      TargetRange_ChatMessage("Range Window Enabled");
    else
      TargetRange_ChatMessage("Range Window Disabled");
    end

    if vardb.targetShow then
      TargetRange_ChatMessage("Target's Target Window Enabled");
    else
      TargetRange_ChatMessage("Target's Target Window Disabled");
    end
  else
    TargetRange_ChatMessage("Disabled");
  end
	
end


function TargetRange_GetTextWidth(text)

  TargetRangeWatchFrame2WidthTest:SetText(text);
  return TargetRangeWatchFrame2WidthTest:GetWidth();

end


function TargetRange_Center()

  vardb.locked = false;
  TargetRange_Unlock(true);
  TargetRangeWatchFrame:ClearAllPoints();
  TargetRangeWatchFrame:SetPoint("CENTER", UIParent, "CENTER");
  TargetRangeWatchFrameStatusBar1:ClearAllPoints();
  TargetRangeWatchFrameStatusBar1:SetPoint("TOP", TargetRangeWatchFrame, "TOP");
  TargetRangeWatchFrameStatusBar2:ClearAllPoints();
  TargetRangeWatchFrameStatusBar2:SetPoint("TOP", TargetRangeWatchFrameStatusBar1, "BOTTOM");
  if vardb.targetShow == false and vardb.rangeShow == false then
    vardb.targetShow = true;
    vardb.rangeShow = true;
    TargetRange_SetupUser();
  end

end

function TargetRange_Lock(PrintMsg)

  if PrintMsg then
    TargetRange_ChatMessage("Frame Locked");
  end
  TargetRangeFade() ;
  TargetRangeWatchFrame:EnableMouse(0);

end


function TargetRange_Unlock(PrintMsg)

  if PrintMsg then
    TargetRange_ChatMessage("Frame Unlocked");
  end
  TargetRangeFrame:Show();
  TargetRangeWatchFrame:Show();
  TargetRangeWatchFrame:EnableMouse(1);

end


function TargetRange_IsLocked()

  return vardb.locked;

end


function TargetRange_SetTextHeight(aFontHeight)

  HRDebug("func: TargetRange_SetTextHeight - "..aFontHeight);
  local font = GameFontNormalSmall:GetFont();
  if aFontHeight then
      aFontHeight = tonumber(aFontHeight);
      if aFontHeight == nil then 
        aFontHeight = vardb.textHeight;
      end
      if aFontHeight > 0 and aFontHeight < 31 then
        vardb.textHeight = aFontHeight;
	  getglobal("TargetRangeWatchFrameStatusBar1String1"):SetFont(font, aFontHeight, "OUTLINE");
	  getglobal("TargetRangeWatchFrameStatusBar2String2"):SetFont(font, aFontHeight, "OUTLINE");

	  TargetRangeWatchFrame2WidthTest:SetFont(font, aFontHeight);

	else
        TargetRange_ChatMessage(TARGETRANGE_TEXTHEIGHT_ERROR);
      end
  
  else
      TargetRange_ChatMessage(TARGETRANGE_TEXTHEIGHT_ERROR);
  end
  TargetRangeWatchFrameStatusBar1:SetHeight((vardb.textHeight)+ 16);
  TargetRangeWatchFrameStatusBar2:SetHeight((vardb.textHeight)+ 16);

  TargetRange_TargetSize(vardb.targetLength);

end

function TargetRange_SetTargetSize(aTargetLength)

  aTargetLength = tonumber(aTargetLength);
  if aTargetLength then
    vardb.targetLength = aTargetLength;
    if vardb.targetLength <= 1 then
      vardb.targetLength = 1;
    elseif vardb.targetLength >= 30 then
      vardb.targetLength = 30;
    end
  end
  TargetRange_TargetSize(vardb.targetLength);

end


function TargetRange_SetRangeSize(aRangeLength)

  if aRangeLength then
    vardb.rangeLength = aRangeLength;
    if vardb.rangeLength <= 1 then
      vardb.rangeLength = 1;
    elseif vardb.rangeLength >= 30 then
      vardb.rangeLength = 30;
    end
  end
  TargetRange_RangeSize();

end


function TargetRange_SetAlpha(aAlphaMsg)

  aAlphaMsg = tonumber(aAlphaMsg);
  if aAlphaMsg then
    vardb.alpha = aAlphaMsg;
    if vardb.alpha >= 1.0 then
      vardb.alpha = 1.0;
    elseif vardb.alpha <= 0.0 then
      vardb.alpha = 0.0;
    end

    TargetRangeWatchFrameStatusBar1:SetBackdropColor(0, 0, 0, vardb.alpha);
    TargetRangeWatchFrameStatusBar2:SetBackdropColor(0, 0, 0, vardb.alpha);
    TargetRangeWatchFrameStatusBar1:SetBackdropBorderColor(1.0, 1.0, 1.0, vardb.alpha);
    TargetRangeWatchFrameStatusBar2:SetBackdropBorderColor(1.0, 1.0, 1.0, vardb.alpha);
    local r, g, b, a = TargetRangeWatchFrameStatusBar1:GetStatusBarColor();
    TargetRangeWatchFrameStatusBar1:SetStatusBarColor(r, g, b, vardb.alpha);
    r, g, b, a = TargetRangeWatchFrameStatusBar2:GetStatusBarColor();
    TargetRangeWatchFrameStatusBar2:SetStatusBarColor(r, g, b, vardb.alpha);

  end

end

function TargetRange_SetUpdateInterval(aUpdateInterval)

  aUpdateInterval = tonumber(aUpdateInterval);
  if aUpdateInterval then
    vardb.updateInterval = aUpdateInterval;
    vardb.updateInterval = max(0, vardb.updateInterval);
  end

end

function HRDebug(aDebug)

  if varDebug then
    DEFAULT_CHAT_FRAME:AddMessage("|cffff0000" .. TARGETRANGE_NAME .. "Debug: " .. aDebug);
  end

end


function TargetRange_ChatMessage(msg)

  DEFAULT_CHAT_FRAME:AddMessage("|cffffff00" .. TARGETRANGE_NAME .. ": " .. msg);

end




-- ************************************************
--** Options .xml functions 
--*************************************************

function TargetRange_OptionsLoad()

  TargetRangeOptionsTargetWindowToggle:SetChecked(TargetRange_BoolConvert1(vardb.targetShow));
  TargetRangeOptionsRangeWindowToggle:SetChecked(TargetRange_BoolConvert1(vardb.rangeShow));
  TargetRangeOptionsLockToggle:SetChecked(TargetRange_BoolConvert1(vardb.locked));
  TargetRangeOptionsFadeToggle:SetChecked(TargetRange_BoolConvert1(vardb.windowFade));  
  TargetRangeOptionsFriendlyFadeToggle:SetChecked(TargetRange_BoolConvert1(vardb.windowFriendlyFade));  
  TargetRangeOptionsRangeTextToggle:SetChecked(TargetRange_BoolConvert1(vardb.rangeText));
  TargetRangeOptionsClassColorToggle:SetChecked(TargetRange_BoolConvert1(vardb.showClassColor));
  TargetRangeOptionsTargetSizeSlider:SetValue(vardb.targetLength);
  TargetRangeOptionsRangeSizeSlider:SetValue(vardb.rangeLength);
  TargetRangeOptionsFontSizeSlider:SetValue(vardb.textHeight);
  TargetRangeOptionsAlphaSlider:SetValue(vardb.alpha * 100);

end


function TargetRange_OptionsCenter()

  TargetRange_Center();
  TargetRange_OptionsLoad();

end


function TargetRange_OptionsLock()

  if (TargetRange_BoolConvert2(TargetRangeOptionsLockToggle:GetChecked()) == true) then
    vardb.locked = true;
    TargetRange_Lock(true);
  else
    vardb.locked = false;
    TargetRange_Unlock(true);
  end

end

function TargetRange_OptionsClassColor()

  vardb.showClassColor = TargetRange_BoolConvert2(TargetRangeOptionsClassColorToggle:GetChecked());
  TargetRange_SetupUser();

end

function TargetRange_OptionsRange()

  vardb.rangeShow = TargetRange_BoolConvert2(TargetRangeOptionsRangeWindowToggle:GetChecked());
  if (vardb.rangeShow == false) then
    TargetRange_ChatMessage("Range Window Disabled");
  else
    TargetRange_ChatMessage("Range Window Enabled");
  end
  TargetRange_SetupUser();

end


function TargetRange_OptionsTarget()

  vardb.targetShow = TargetRange_BoolConvert2(TargetRangeOptionsTargetWindowToggle:GetChecked());
  if (vardb.targetShow == false) then
    TargetRange_ChatMessage("Target's Target Window Disabled");
  else
    TargetRange_ChatMessage("Target's Target Window Enabled");
  end
  TargetRange_SetupUser();

end


function TargetRange_OptionsFade()

  vardb.windowFade = TargetRange_BoolConvert2(TargetRangeOptionsFadeToggle:GetChecked());
  if (vardb.windowFade == true) then
    TargetRange_ChatMessage("Window Fade Enabled");
    if (vardb.locked == false) then 
      TargetRange_ChatMessage("Must lock targetrange window for fade to work");     
    end
  else
    TargetRange_ChatMessage("Window Fade Disabled");
  end
  TargetRange_SetupUser();

end


function TargetRange_OptionsFriendlyFade()

  vardb.windowFriendlyFade = TargetRange_BoolConvert2(TargetRangeOptionsFriendlyFadeToggle:GetChecked());
  if (vardb.windowFriendlyFade == true) then
    TargetRange_ChatMessage("Friendly Window Fade Enabled");
    if (vardb.locked == false) then 
      TargetRange_ChatMessage("Must type /targetrange lock for friendly window fade to work");
    end
  else
    TargetRange_ChatMessage("Friendly Window Fade Disabled");
  end
  TargetRange_SetupUser();

end

function TargetRange_OptionsRangeText()

  if (TargetRange_BoolConvert2(TargetRangeOptionsRangeTextToggle:GetChecked()) == true) then
    vardb.rangeText = true;
  else
    vardb.rangeText = false;
  end

end


function TargetRange_InitRangeSizeSlider(slider)

  getglobal(slider .. "Low"):SetText("1");
  getglobal(slider .. "High"):SetText("30"); 

end


function TargetRange_OptionsRangeSizeSlider()

  if (vardb.rangeLength ~= nil) then
    TargetRange_SetRangeSize(TargetRangeOptionsRangeSizeSlider:GetValue());
  end

end


function TargetRange_InitTargetSizeSlider(slider)

  getglobal(slider .. "Low"):SetText("1");
  getglobal(slider .. "High"):SetText("30"); 

end


function TargetRange_OptionsTargetSizeSlider()

  if (vardb.targetLength ~= nil) then
    TargetRange_SetTargetSize(TargetRangeOptionsTargetSizeSlider:GetValue());
  end

end


function TargetRange_InitFontSlider(slider)

  getglobal(slider .. "Low"):SetText("1");
  getglobal(slider .. "High"):SetText("30"); 

end


function TargetRange_OptionsFontSizeSlider()

  if (vardb.textHeight ~= nil) then
    TargetRange_SetTextHeight(TargetRangeOptionsFontSizeSlider:GetValue());
  end

end


function TargetRange_InitAlphaSlider(slider)

  getglobal(slider .. "Low"):SetText("0%");
  getglobal(slider .. "High"):SetText("100%");

end


function TargetRange_OptionsAlphaSlider()

  if (vardb.alpha ~= nil) then
    TargetRange_SetAlpha(TargetRangeOptionsAlphaSlider:GetValue() * .01);
  end

end


function TargetRange_OptionsClose()

  TargetRange_SetupUser();
  TargetRangeOptions:Hide();

end


function TargetRange_BoolConvert1(val)
	if (val == false) then
		return nil;
	else
		return 1;
	end
end


function TargetRange_BoolConvert2(val)
	if (val) then
		return true;
	else
		return false;
	end
end

--function round (n,shift)
--	shift = 10^shift;
--	return math.floor ((n*shift)+0.5)/shift;
--end
